module("framework.res.PrefabInstantiate", package.seeall)

---@class PrefabInstantiate : LuaCompBase
local PrefabInstantiate = class("PrefabInstantiate", LuaCompBase)

--加载单个prefab，创建实例，挂在containerGO上
function PrefabInstantiate.Create(containerGO)
    return MonoHelper.addNoUpdateLuaComOnceToGo(containerGO, PrefabInstantiate)
end

function PrefabInstantiate:init(containerGO)
    self._containerGO = containerGO
    self._path = nil
    self._assetItem = nil
    self._instGO = nil
    self._finishCallback = nil
    self._callbackTarget = nil
end

function PrefabInstantiate:startLoad(path, finishCallback, callbackTarget)
    self._path = path
    self._finishCallback = finishCallback
    self._callbackTarget = callbackTarget
    loadAbAsset(path, false, self._onLoadCallback, self)
end

function PrefabInstantiate:getPath()
    return self._path
end

function PrefabInstantiate:getAssetItem()
    return self._assetItem
end

--获取prefab的实例GameObject，如果没加载完，会返回nil
function PrefabInstantiate:getInstGO()
    return self._instGO
end

function PrefabInstantiate:dispose()
    if self._path then
        removeAssetLoadCb(self._path, self._onLoadCallback, self)
    end
    if self._assetItem then
        self._assetItem:Release()
    end
    gohelper.destroy(self._instGO)
    self._path = nil
    self._assetItem = nil
    self._instGO = nil
    self._finishCallback = nil
    self._callbackTarget = nil
end

function PrefabInstantiate:onDestroy()
    self:dispose()
    self:__onDispose()
end

function PrefabInstantiate:_onLoadCallback(assetItem)
    if assetItem.IsLoadSuccess then
        self._assetItem = assetItem
        self._assetItem:Retain()
        self._instGO = gohelper.clone(self._assetItem:GetResource(), self._containerGO)
        if self._finishCallback then
            if self._callbackTarget then
                self._finishCallback(self._callbackTarget, self)
            else
                self._finishCallback(self)
            end
        end
    end
end

return PrefabInstantiate
